package com.xbongbong.parent.util;

import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.DateTimeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.PaasDateUtil;
import com.xbongbong.parent.pojo.RangeTimePoJo;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/11/30 14:16
 */
public class RangeTimeUtil {


    public static RangeTimePoJo getRangeTime(FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, String symbol, Long dateInt) throws XbbException {

        if (Objects.isNull(fieldAttrEntity) || Objects.isNull(targetField)) {
            // 避免字段删除了，导致其空指针
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.THIS_EXPLAIN_NOT_EXIST);
        }
        if (Objects.isNull(dateInt)) {
            return null;
        }
        boolean same = (Objects.equals(fieldAttrEntity.getDateType(), DateTimeEnum.DATETIME.getType())
                && Objects.equals(targetField.getDateType(), DateTimeEnum.DATETIME.getType()))
                || (Objects.equals(fieldAttrEntity.getDateType(), DateTimeEnum.TIME.getType())
                && Objects.equals(targetField.getDateType(), DateTimeEnum.TIME.getType()));

        if (Objects.equals(fieldAttrEntity.getDateType(), DateTimeEnum.TIME.getType())
                && Objects.equals(targetField.getDateType(), DateTimeEnum.DATETIME.getType())) {
            // 「日期」和「日期时间」可以互相对比。「日期」和「日期时间」字段对比时，仅日期相等即可。
            Long beforeDate = PaasDateUtil.getBeforeDayBreak(dateInt);
            Long endDate = PaasDateUtil.getDayLastSecond(dateInt);

            RangeTimePoJo rangeTimePoJo = RangeTimeUtil.getTime(symbol, beforeDate, endDate);
            return rangeTimePoJo;
        } else if (Objects.equals(fieldAttrEntity.getDateType(), DateTimeEnum.DATETIME.getType())
                && Objects.equals(targetField.getDateType(), DateTimeEnum.TIME.getType())) {
            // 「日期时间」和「日期」字段对比时。「日期时间」字段的时间应该为00:00:00，才能相等
            Long beforeDate = PaasDateUtil.getBeforeDayBreak(dateInt);

            return new RangeTimePoJo(Arrays.asList(beforeDate), symbol);
        } else if (same) {
            // // 「日期时间」和「日期时间」可以互相对比。因为没有精确到00秒，所以只能检索00秒到59秒。
            Long beforeDate = PaasDateUtil.getDateFirstSecond(dateInt);
            Long endDate = PaasDateUtil.getDateLastSecond(dateInt);
            RangeTimePoJo rangeTimePoJo = RangeTimeUtil.getTime(symbol, beforeDate, endDate);
            return rangeTimePoJo;
        }
        return new RangeTimePoJo(Arrays.asList(dateInt), symbol);
    }

    public static RangeTimePoJo getTime(String symbol, Long beforeDate, Long endDate) {
        List<Object> dateList = new ArrayList<>();
        if (Objects.isNull(beforeDate) || Objects.isNull(endDate)) {
            return new RangeTimePoJo(null, symbol);
        }
        if (Objects.equals(ConditionEnum.EQUAL.getSymbol(), symbol)) {
            symbol = ConditionEnum.RANGE.getSymbol();
            dateList.add(beforeDate);
            dateList.add(endDate);
        }else if (Objects.equals(ConditionEnum.LESSEQUAL.getSymbol(), symbol)
                || Objects.equals(ConditionEnum.LESSTHAN.getSymbol(), symbol)) {
            dateList.add(endDate);
        } else if (Objects.equals(ConditionEnum.GREATEREQUAL.getSymbol(), symbol)
                || Objects.equals(ConditionEnum.GREATERTHAN.getSymbol(), symbol)) {
            dateList.add(beforeDate);
        }else {
            dateList.add(beforeDate);
        }
        return new RangeTimePoJo(dateList, symbol);
    }

}
